package com.weapons.week0903_weapons.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.adapter.VpAdapter;
import com.weapons.week0903_weapons.constant.UrlString;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ForumFragment extends Fragment {
    private TabLayout tl_bbs;
    private ViewPager vp_bbs;
    private String[] mTitle = {"猎奇","历史","娱乐","网友"};
    private List<Fragment> mFragment;
    private Activity mActivity;
    private String mpos="";

    public ForumFragment newInstance(String pos) {
        ForumFragment fragment=new ForumFragment();
        Bundle bundle=new Bundle();
        bundle.putString("pos",pos);
        fragment.setArguments(new Bundle(bundle));
        return fragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity=getActivity();
        Bundle bundle=getArguments();
        if (bundle!=null){
            mpos=bundle.getString("pos");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_forum, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tl_bbs = (TabLayout) view.findViewById(R.id.tl_bbs);
        vp_bbs = (ViewPager) view.findViewById(R.id.vp_bbs);

        mFragment = new ArrayList<>();
        initFragment();
        vp_bbs.setAdapter(new VpAdapter(getChildFragmentManager(),mActivity,mFragment));
        tl_bbs.setupWithViewPager(vp_bbs);
        int tabCount = tl_bbs.getTabCount();
        for (int i = 0; i < tabCount ; i++) {
            TabLayout.Tab tab = tl_bbs.getTabAt(i);
            tab.setText(mTitle[i]);
        }
    }

    private void initFragment() {
        mFragment.add(new BbsFragment().newInstance(UrlString.URL_FORUM_CURIOUSE));
        mFragment.add(new BbsFragment().newInstance(UrlString.URL_FORUM_HISTORY));
        mFragment.add(new BbsFragment().newInstance(UrlString.URL_FORUM_RECREATION));
        mFragment.add(new BbsFragment().newInstance(UrlString.URL_FORUM_NETFRIEND));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


}
